package com.ashin.controller;

import com.ashin.entity.MovieEntity;
import com.ashin.repository.MovieRepository;
import lombok.Data;
import org.neo4j.driver.Driver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ashin
 */
@RestController
@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private Driver driver;


    @PutMapping("/")
    public MovieEntity createOrUpdateMovie(@RequestBody MovieEntity newMovie) {
        return movieRepository.save(newMovie);
    }

    @GetMapping(value = {"", "/"})
    public Iterable<MovieEntity> getMovies() {
        Iterable<MovieEntity> all = movieRepository.findAll();
        System.out.println(all);
        System.out.println(driver);
        return all;
    }

    @GetMapping(value = {"/all", "/"})
    public List<MovieEntity> all() {
        List<MovieEntity> all = movieRepository.all();
        System.out.println(all);
        return all;
    }

    @GetMapping("/by-title")
    public MovieEntity byTitle(@RequestParam String title) {
        return movieRepository.findOneByTitle(title);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        movieRepository.deleteById(id);
    }

    @GetMapping("/queryTitle")
    public List<String> queryTitle(@RequestBody QueryEntity queryEntity) {
        List<String> title = movieRepository.findTitle(queryEntity.getStartR(), queryEntity.getEndR());
        return title;
    }

    @Data
    public static class QueryEntity {

        private Integer startR;

        private Integer endR;
    }

}
